package com.zzyl.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description DeviceDataServiceImpl
 * @Author apeng
 * @Date 2024-11-02
 */
@Service
@RequiredArgsConstructor
public class DeviceDataServiceImpl implements DeviceDataService {

    private final DeviceDataMapper deviceDataMapper;

    @Override
    public PageResponse<DeviceDataVo> getDeviceDataPage(DeviceDataPageReqDto dto) {
        LocalDateTime startDate = LocalDateTimeUtil.of(dto.getStartTime());
        LocalDateTime endDate = LocalDateTimeUtil.of(dto.getEndTime());
        dto.setStartDate(startDate);
        dto.setEndDate(endDate);
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
       Page<DeviceDataVo> page = deviceDataMapper.getDeviceDataPage(dto);
        PageResponse<DeviceDataVo> pageResponse = PageResponse.of(page, DeviceDataVo.class);
        return pageResponse;
    }

    /**
     * 按日查询设备数据
     * @param iotId
     * @param functionId
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByDay(String iotId,
                                                            String functionId,
                                                            Long startTime,
                                                            Long endTime) {
        LocalDateTime startDate = LocalDateTimeUtil.of(startTime);
        LocalDateTime endDate = LocalDateTimeUtil.of(endTime);
        //从数据库中获取数据
        List<DeviceDataGraphVo> deviceDataGraphVos = deviceDataMapper.queryDeviceDataListByDay(iotId,functionId,startDate,endDate);
        //将list转成map集合
        Map<String, Double> map = null;

        if (CollectionUtil.isNotEmpty(deviceDataGraphVos)){
          map = deviceDataGraphVos.stream()
                    .collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));
        }

        //使用工具类获得24小时的数据
        List<DeviceDataGraphVo> voList = DeviceDataGraphVo.dayInstance(startDate);
        for (DeviceDataGraphVo vo : voList) {
            Double value = map.get(vo.getDateTime());
            vo.setDataValue(value);
        }
        return voList;
    }

    /**
     * 按周查询设备数据
     * @param iotId
     * @param functionId
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByWeek(String iotId, String functionId, Long startTime, Long endTime) {
        LocalDateTime startDate = LocalDateTimeUtil.of(startTime);
        LocalDateTime endDate = LocalDateTimeUtil.of(endTime);
        //从数据库中获取数据
        List<DeviceDataGraphVo> deviceDataGraphVos = deviceDataMapper.queryDeviceDataListByWeek(iotId,functionId,startDate,endDate);
        //将list转成map集合
        Map<String, Double> map = null;

        if (CollectionUtil.isNotEmpty(deviceDataGraphVos)){
            map = deviceDataGraphVos.stream()
                    .collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));
        }

        //使用工具类获得24小时的数据
        List<DeviceDataGraphVo> voList = DeviceDataGraphVo.weekInstance(startDate);
        for (DeviceDataGraphVo vo : voList) {
            Double value = map.get(vo.getDateTime());
            vo.setDataValue(value);
        }
        return voList;
    }
}
